<?php
/**
 *   This file is part of GeoArticle (a Joomla plugin).
 *   Written by Ivar Bergman (www.ivarbergman.se)
 *   Copyright (c) 2010 Ivar Bergman. All rights reserved.
 *
 *   GeoArticle is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   GeoArticle is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with GeoArticle.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

jimport( 'joomla.application.component.view');
 
class GeoArticleViewGeoArticleTag extends JView
{
  const html_option  = '<option value="%d"%s>%s</option>';
  
  function display($tpl = null)
  {
    global $mainframe;

    if($this->getLayout() == 'form') {
      $this->_displayForm($tpl);
      return;
    }

    $record = &$this->get('data');

    parent::display($tpl);
  }

  function _displayForm($tpl)
  {
    global $mainframe, $option;

    $db	   =& JFactory::getDBO();
    $uri   =& JFactory::getURI();
    $user  =& JFactory::getUser();
    $model =& $this->getModel();

    $lists = array();

    $record	=& $this->get('data');
    $isNew	= ($record->id < 1);
        
    // fail if checked out not by 'me'
    if ($model->isCheckedOut( $user->get('id') )) {
      $msg = JText::sprintf( 'DESCBEINGEDITTED', JText::_( 'The Tag' ), $record->title );
      $mainframe->redirect( 'index.php?option='. $option, $msg );
    }

    $children_count = NULL;
    
    // Edit or Create?
    if (!$isNew) {
      $model->checkout( $user->get('id') );
          
      $db->setQuery(
		    sprintf(
			    'SELECT COUNT(cid) AS num_children FROM #__content_geoarticle WHERE gaid = %d;',
			    (int) $record->id
			    )
		    );
      
      $children_count = $db->loadObject();
      
      $lists['children_count'] = $children_count->num_children;
    }
    else {
      // initialise new record
      $record->published = 1;
      $record->parentid = 0;
      $record->description = NULL;
      $record->title = NULL;
      $record->state = 1 ;
      
      $lists['children_count'] = 0;
    }

    // Ordering:
    $lists['ordering'] = ( $isNew ) ?  
      '<input type="hidden" name="ordering" value="'. $record->ordering .'" />'. JText::_( 'DESCNEWITEMSLAST' ) : 
      JHTML::_('list.specificordering',  $record, $record->id, 'SELECT ordering AS value, title AS text FROM #__geoarticle ORDER BY ordering' );

    // build the html select list
    $lists['published']  = JHTML::_('select.booleanlist',  'state', 'class="inputbox"', $record->state );
    $lists['created_by'] = JHTML::_('list.users', 'created_by', true);
    
    $this->assignRef('lists',		$lists);
    $this->assignRef('record',	$record);

    parent::display($tpl);
  }
}
